package com.seadragon.app.ksr.validator;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.seadragon.app.ksr.model.Address;
import com.seadragon.app.ksr.model.Office;
import static com.seadragon.app.ksr.model.KsrConstants.ERROR_MESSAGE_KEY_FIELD_REQUIRED;
import static com.seadragon.app.ksr.model.KsrConstants.DEFAULT_MESSAGE_KEY_INVALID_EMAIL;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_EMAIL;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_PHONE_NUMBER;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_SPECIALTY;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_DESCRIPTION;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_PRICE;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_ADDRESS;
import static com.seadragon.app.ksr.model.KsrConstants.ERROR_MESSAGE_KEY_PRICE_POSITIVE;

public class OfficeValidator implements Validator {
	private final Validator addressValidator;
	public OfficeValidator(Validator addressValidator) {
        if (addressValidator == null) {
            throw new IllegalArgumentException(
              "The supplied [Validator] is required and must not be null.");
        }
        if (!addressValidator.supports(Address.class)) {
            throw new IllegalArgumentException(
              "The supplied [Validator] must support the validation of [Address] instances.");
        }
        this.addressValidator = addressValidator;
	}	
	@Override
	public boolean supports(Class<?> clazz) {
		return Office.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		Office office = (Office) target;
		if (office.getPrice()<=0)
			errors.reject(FIELD_NAME_PRICE,ERROR_MESSAGE_KEY_PRICE_POSITIVE);
		if (StringUtils.isEmpty(office.getPhoneNumber()))
			errors.reject(FIELD_NAME_PHONE_NUMBER,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(office.getSpecialty()))
			errors.reject(FIELD_NAME_SPECIALTY,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(office.getDescription()))
			errors.reject(FIELD_NAME_DESCRIPTION,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(office.getEmailAddress())){
			errors.reject(FIELD_NAME_EMAIL,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		} else if(!EmailValidator.getInstance().isValid(office.getEmailAddress())){
			errors.reject(FIELD_NAME_EMAIL,DEFAULT_MESSAGE_KEY_INVALID_EMAIL);
		}		
		if (office.getAddress()==null){
			errors.reject(FIELD_NAME_ADDRESS,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		}	
        try {
        	if (office.getAddress()!=null){
        		errors.pushNestedPath("address");
        		ValidationUtils.invokeValidator(this.addressValidator, office.getAddress(), errors);
        	}
        } finally {
            errors.popNestedPath();
        }			
	}

}
